//
//  YGRegisterPhoneTableViewCell.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/3/16.
//

#import "YGRegisterPhoneTableViewCell.h"
#import "JGCountDownButton.h"
#import "YGRegisterBLL.h"
@interface YGRegisterPhoneTableViewCell ()
@property (nonatomic, strong) JGCountDownButton *countdownBtn;    //验证码
@property (nonatomic, strong) UITextField *phoneTextField;         //手机号






@end
@implementation YGRegisterPhoneTableViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
        [self initContraints];
    }
    return self;
}

- (void)initView{

    [self addContentView:self.phoneTextField];
    [self addContentView:self.countdownBtn];

}

- (void)initContraints{
    [self.phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(self.contentSuperView.mas_height);
        make.width.mas_equalTo(150);
        make.left.mas_equalTo(self.contentSuperView.mas_left);
    }];
    [self.countdownBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.phoneTextField.mas_right).offset(10);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(self.contentSuperView.mas_height);
    }];
}



- (void)clickAuthBtnAction{
    if (self.phoneTextField.text.length != 11) {
        //非11位
//        [self showTip:@"请输入正确的手机号"];
        return ;
    }
    WeakSelf(self);
    [YGRegisterBLL requstAuthCodeWithType:YGAuthCodeRegisterType phone:self.phoneTextField.text success:^{
        [weakself.countdownBtn startDown];
    }];
}
#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField == self.phoneTextField) {
        NSString *regex =@"[0-9]*";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
        if ([pred evaluateWithObject:string]) {
            return YES;
        }
        return NO;
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
//    [self onLoginBtnClicked:self.loginButton];
    return YES;
}

- (void)textFieldDidChange:(UITextField *)theTextField{
    if (theTextField.text.length == 11) {
        if (self.CallBack) {
            self.CallBack(theTextField.text, self.tag);
        }
    }
}

- (UITextField *)phoneTextField {
    if (!_phoneTextField) {
        _phoneTextField = [[UITextField alloc] init];
        _phoneTextField.font = [UIFont systemFontOfSize:14];
        _phoneTextField.layer.borderWidth = 0;
        _phoneTextField.placeholder = @"请输入手机号码";
        _phoneTextField.textAlignment = NSTextAlignmentLeft;
        _phoneTextField.textAlignment = NSTextAlignmentLeft;
        _phoneTextField.delegate = self;
        [_phoneTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
//
//        UIView *underline=[[UIView alloc] init];
//        underline.backgroundColor=UIColorFromRGB(0xf5f5f5);
//        [self.phoneTextField addSubview:underline];
//        
//        [underline mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.mas_equalTo(self.phoneTextField.mas_bottom).offset(-1);
//            make.left.mas_equalTo(self.phoneTextField.mas_left);
//            make.height.mas_equalTo(1);
//            make.right.mas_equalTo(self.phoneTextField.mas_right);
//        }];
    }
    return _phoneTextField;
}



- (JGCountDownButton *)countdownBtn
{
    if (!_countdownBtn) {
        _countdownBtn = [JGCountDownButton countDownButtonWithTitle:@"获取验证码"
                                                     countDownTitle:@"秒后重试"
                                                         titleColor:UIColorFromRGB(0x37C8D7)
                                                countDownTitleColor:UIColorFromRGB(0x333333)];
        _countdownBtn.time = 60;
        [_countdownBtn addTarget:self action:@selector(clickAuthBtnAction)
                forControlEvents:UIControlEventTouchUpInside];
    }
    return _countdownBtn;
}


@end
